import json

from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.models import Group
from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
from django.core.paginator import Paginator

from rest_framework import viewsets, views, permissions, generics, status
from rest_framework_simplejwt.tokens import Token, AccessToken
from rest_framework_simplejwt.views import TokenObtainPairView
from django.contrib.auth.decorators import login_required

from .models import User, Product, Category, Item, Cart
from .serializers import *


# Create your views here.

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
    permission_classes = [permissions.IsAuthenticated]

class GroupViewSet(viewsets.ModelViewSet):
    queryset = Group.objects.all().order_by('name')
    serializer_class = GroupSerializer
    permission_classes = [permissions.IsAuthenticated]
class LoginView(TokenObtainPairView):
    serializer_class =LoginSerializer
    

# @login_required
def user_info(request):
    try:
        token_str = request.headers['Authorization'].split()[1]
        token = AccessToken(token_str)
        pk = token.get('user_id')
        return HttpResponseRedirect("/users/"+str(pk))
    except KeyError:
        return HttpResponse(json.dumps({"detail":"Unauthorized"}),status=401)

class CategoryViewSet(viewsets.ModelViewSet):
    queryset = Category.objects.all()
    serializer_class = CatogorySerializer
    permission_classes = [permissions.IsAuthenticated]

class ProductViewSet(viewsets.ModelViewSet):
    serializer_class = ProductSerializer
    permission_classes = [permissions.IsAuthenticated]

    def get_queryset(self):
        print(self.request.data)
        category_id = self.request.data.get("category")
        ordering = self.request.data.get("ordering")
        page_size = self.request.data.get("page_size")
        page_number = self.request.data.get("page")
        category = Category.objects.filter(id=category_id)[0]

        print(category,ordering,page_size,page_number)
        products = Product.objects.filter(category=category).order_by(ordering)
        print(products)
        paginator = Paginator(products, page_size)
        page_obj = paginator.get_page(page_number)
        print(page_obj.object_list)
        return page_obj.object_list

class ItemViewSet(viewsets.ModelViewSet):
    serializer_class = ItemSerializer
    permission_classes = [permissions.IsAuthenticated]

    def get_queryset(self):
        return Item.objects.filter(customer=self.request.user)


class CartViewSet(viewsets.ModelViewSet):
    serializer_class = CartSerializer
    permission_classes = [permissions.IsAuthenticated]

    queryset = Cart.objects.all()

class AddressViewSet(viewsets.ModelViewSet):
    serializer_class = AddressSerializer
    permission_classes = [permissions.IsAuthenticated]

    def get_queryset(self):
        return Address.objects.filter(customer=self.request.user)


class ExpressViewSet(viewsets.ModelViewSet):
    serializer_class = ExpressSerializer
    permission_classes = [permissions.IsAuthenticated]
    queryset = Express.objects.all()

class PayViewSet(viewsets.ModelViewSet):
    serializer_class = PaySerializer
    permission_classes = [permissions.IsAuthenticated]
    queryset = Pay.objects.all()

class OrderViewSet(viewsets.ModelViewSet):
    serializer_class = OrderSerializer
    permission_classes = [permissions.IsAuthenticated]

    def get_queryset(self):
        return Order.objects.filter(customer=self.request.user)